package com.snda.lib.task;

import java.util.HashMap;
import java.util.Map;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.HttpConst;
import com.snda.lib.http.ICallBack;

import android.content.Context;
import android.os.Bundle;

/*
 * 下载文件的基类
 */
public class DownloadAsyncTask extends HttpGetTask {

	protected String strDownloadFileName = ""; // 下载的文件的名称
	protected String strSavePath = ""; // 下载的文件的保存路径
	protected Boolean bContinue = true;

	public DownloadAsyncTask(Context context, ICallBack callback) {
		super(context, callback);

		this.nTaskType = HttpConst.TASKTYPE_DOWNLOADFILE;
	}

	public void setSavePath(String strPath) {
		this.strSavePath = strPath;
	}

	public void setContinue(boolean bContinue) {
		this.bContinue = bContinue;
	}

	/**
	 * @desc 实际的完成后台任务的地方
	 */
	@Override
	protected Map<String, Object> doTask(Object[] params) {

		// 获取下载的文件名
		int nIndex = strUrl.lastIndexOf('/');
		if (nIndex != -1) {
			strDownloadFileName = strUrl.substring(nIndex + 1);
		} else {
			nIndex = strUrl.lastIndexOf('\\');
			if (nIndex != -1) {
				strDownloadFileName = strUrl.substring(nIndex + 1);
			}
		}

		Map<String, Object> mapData = httpRequest.doGetFile(context, strUrl,
				strSavePath, strDownloadFileName, bContinue);
		return mapData;
	}

	@Override
	protected Map<String, Object> doInBackground(Object... params) {

		if (params.length != 2) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_INVALID_PARAM);
			return process(mapData);
		}

		if (params[0] != null && !Bundle.class.isInstance(params[0])) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put(HttpUtil.KEY_ERROR_CODE, HttpUtil.ERROR_INVALID_PARAM);
			return process(mapData);
		}

		if (params[1] != null && Integer.class.isInstance(params[1])) {
			nCookie = (Integer) params[1];
		}

		httpRequest = new HttpUtil(context);
		httpRequest.setListener(this);
		mParams = (Bundle) params[0];
		if (mParams == null) {
			mParams = new Bundle();
		}

		for (String key : mParams.keySet()) {
			httpRequest.addParams(key, mParams.getString(key));
		}

		Map<String, Object> mapData = doTask(params);
		return process(mapData);
	}

	protected Map<String, Object> process(Map<String, Object> mapData) {
		if (mapData == null)
			return null;

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null) {
			setErr(HttpUtil.ERROR_UNKNOWN, mapData);
			return mapData;
		}

		if (nError != HttpUtil.ERROR_SUCCESS
				&& nError != HttpUtil.ERROR_ALREADY_EXIST) {
			setErr(nError, mapData);
			return mapData;
		}

		String strSavePath = (String) mapData.get(HttpUtil.KEY_PATH);
		if (strSavePath == null) {
			setErr(HttpUtil.ERROR_INVALID_SAVE_PATH, mapData);
			return mapData;
		}

		mapData.put(HttpUtil.KEY_TASK_TYPE, nTaskType);

		setErr(nError, mapData);
		mapData.put(HttpUtil.KEY_PATH, strSavePath);
		return mapData;
	}
}
